package com.github.tangmonkmeat;

import java.util.Arrays;
import java.util.Comparator;

/**
 * Description:
 *
 * @author zwl
 * @version 1.0
 * @date 2021/8/4 上午9:26
 */
public class 字符串相乘 {

    public static void main(String[] args) {
        //String multiply = multiply("0", "140");
        //System.out.println(multiply);

        //int[][] ins = new int[][]{{1,3},{1,2}};

        //Arrays.sort(ins, new Comparator<int[]>() {
        //    @Override
        //    public int compare(int[] o1, int[] o2) {
        //        if (o1[0]==o2[0]) {
        //            return o1[1]-o2[1];
        //        }
        //        return o1[0]-o2[0];
        //    }
        //});

        //System.out.println(ins);

        String str1="abcde";
        String str2= str1.substring (4);
        String str3=new String (str2);

        System.out.println(str3 == str2);
        String str4=new String (str2.toCharArray());

        System.out.println(str4 == str3);
    }

    public static String multiply(String num1, String num2) {

        if (num1.length() == 1 && num2.length() == 1){
            int sum = (num1.charAt(0) - '0') * (num2.charAt(0) - '0');
            return sum + "";
        }

        String res = "0";
        int t = 0;
        char[] cs1;
        char[] cs2;
        if(num2.length() <= num1.length()){
            cs1 = num1.toCharArray();
            cs2 = num2.toCharArray();
        }else{
            cs1 = num2.toCharArray();
            cs2 = num1.toCharArray();
        }

        // 从后往前 取cs2的每一位和cs1相乘
        for(int i = cs2.length - 1;i >= 0;i--){
            int cap = 0;
            StringBuilder sb = new StringBuilder();
            int n2 = cs2[i] - '0';
            //
            for(int j = cs1.length - 1;j >= -1;j--){
                if(j == -1){
                    if(cap != 0) {
                        sb.append(cap);
                    }
                }else {
                    int n1 = cs1[j] - '0';
                    int sum = n1 * n2 + cap;
                    int value = sum % 10;
                    cap = sum / 10;
                    sb.append(value);
                }
            }
            res = mul2(res.toCharArray(),sb.reverse().toString().toCharArray(),t++);
        }
        return res.charAt(0) == '0' ? "0" : res;

    }

    public static String mul2(char[] cs1,char[] cs2,int t){
        StringBuilder sb = new StringBuilder();
        if (cs1.length > 0){
            for(int k = 1;k <= t;k++){
                sb.append(cs1[cs1.length - k]);
            }
        }
        int cap = 0;
        int i = cs1.length - t - 1;
        int j = cs2.length - 1;
        for(;i >= 0;i--,j--){
            int n2 = cs2[j] - '0';
            int n1 = cs1[i] - '0';
            int sum = n1 + n2 + cap;
            int value = sum % 10;
            cap = sum / 10;
            sb.append(value);
        }

        j = cs2.length + t - cs1.length - 1;
        for(;j >= -1;j--){
            if(j == -1){
                if(cap != 0) {
                    sb.append(cap);
                }
            }else {
                int sum = cs2[j] - '0' + cap;
                int value = sum % 10;
                cap = sum / 10;
                sb.append(value);
            }
        }
        return sb.reverse().toString();
    }
}
